package com.createidea.cms.struts.action;

import java.io.File;
import java.util.List;

import com.createidea.cms.service.picture.PictureService;
import com.createidea.cms.service.product.ProductService;
import com.createidea.cms.service.producttype.ProductTypeService;
import com.createidea.cms.to.PictureTo;
import com.createidea.cms.to.ProductTo;
import com.createidea.cms.to.ProductTypeTo;
import com.createidea.cms.utils.Pager;

public class ProductAction extends BaseAction {

	private ProductService productService;
	private PictureService pictureService;
	private List<ProductTo> products;
	private ProductTo product;
	private String productId;
	private PictureTo picture;
	private String pictureId;	
	private String producttypeId;
	private List<ProductTypeTo> types;
	private ProductTypeService productTypeService;
	private ProductTypeTo productType;
	
	
	public String maintainProduct() {
		this.pager = new Pager();
		products = productService.getProductsByStatus(1);
		if (products != null && products.size() > 0) {
			this.pager.setTotalNum(products.size());
		} else
			this.pager.setTotalNum(0);
		if (currentPage >= pager.getPageNums().size())
			currentPage = pager.getPageNums().size() - 1;
		if (currentPage <= 0)
			currentPage = 0;
		pager.setCurrentPage(currentPage);
		products = productService.getAllEnableProductsByPager(pager, 1);
		types=productTypeService.getProductTypes();
		return SUCCESS;
	}
	
	public String addProduct(){
		types=productTypeService.getProductTypes();
		return SUCCESS;
		
	}
	
	public String loadProducts(){
		products=productService.getProductsByStatus(1);
		return SUCCESS;
	}
   
	public String saveProduct(){
		productService.saveProcut(product);
		return SUCCESS;
	}
	
	public String deleteProduct(){
		productService.deleteProcut(productId);
		return SUCCESS;
	}
	
	public String editProduct(){
		types=productTypeService.getProductTypes();
		product=productService.getProduct(productId);
		
		return SUCCESS;
	}
	
	public String updateProduct(){
		productService.updateProduct(product);
		return SUCCESS;
	}
			
	public String loadProductsByType() {
		
		this.pager=new Pager();
		products = productService.getProductsForType(1,producttypeId);
		if(products != null && products.size() > 0) {
			this.pager.setTotalNum(products.size());
		} else {
			this.pager.setTotalNum(0);
		}
		if(currentPage>=pager.getPageNums().size())
			currentPage=pager.getPageNums().size()-1;
	   	if(currentPage<=0)
	   		currentPage=0;
	   	pager.setCurrentPage(currentPage);		
	   	types=productTypeService.getProductTypes();
		products = productService.getProductsForTypeInPager(1,producttypeId,pager);
		productType = productTypeService.getProductTypeById(producttypeId);
		return SUCCESS;
	}
	
	public String showProduct() {
		
		product = productService.getProduct(productId);
	    product.setPictures(pictureService.getPicturesForObject(productId));
		return SUCCESS;
	}
	
	public String setPictureAsDefault(){
		productService.setDefaultPicture(pictureId,productId);
		return SUCCESS;
	}
		
	public String loadAllProducts() {
	
		this.pager = new Pager();
		products = productService.getProductsByStatus(1);
		if (products != null && products.size() > 0) {
			this.pager.setTotalNum(products.size());
		} else
			this.pager.setTotalNum(0);
		if (currentPage >= pager.getPageNums().size())
			currentPage = pager.getPageNums().size() - 1;
		if (currentPage <= 0)
			currentPage = 0;
		pager.setCurrentPage(currentPage);
		products = productService.getAllEnableProductsByPager(pager, 1);
		types=productTypeService.getProductTypes();
		return SUCCESS;
	}
	/*
	 * get and set methods
	 */
	

	public ProductService getProductService() {
		return productService;
	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	public List<ProductTo> getProducts() {
		return products;
	}

	public void setProducts(List<ProductTo> products) {
		this.products = products;
	}

	public ProductTo getProduct() {
		return product;
	}

	public void setProduct(ProductTo product) {
		this.product = product;
	}

	public String getProductId() {
		return productId;
	}

	public void setProductId(String productId) {
		this.productId = productId;
	}

	public PictureTo getPicture() {
		return picture;
	}

	public void setPicture(PictureTo picture) {
		this.picture = picture;
	}

	public String getPictureId() {
		return pictureId;
	}

	public void setPictureId(String pictureId) {
		this.pictureId = pictureId;
	}

	public PictureService getPictureService() {
		return pictureService;
	}

	public void setPictureService(PictureService pictureService) {
		this.pictureService = pictureService;
	}

	public String getProducttypeId() {
		return producttypeId;
	}

	public void setProducttypeId(String producttypeId) {
		this.producttypeId = producttypeId;
	}

	public List<ProductTypeTo> getTypes() {
		return types;
	}

	public void setTypies(List<ProductTypeTo> types) {
		this.types = types;
	}

	public ProductTypeService getProductTypeService() {
		return productTypeService;
	}

	public void setProductTypeService(ProductTypeService productTypeService) {
		this.productTypeService = productTypeService;
	}
	
	public void setTypes(List<ProductTypeTo> types) {
		this.types = types;
	}

	public ProductTypeTo getProductType() {
		return productType;
	}

	public void setProductType(ProductTypeTo productType) {
		this.productType = productType;
	}
	
	
}
